store_sample

Owner: Alex

Purpose and Documentation

The A/D conversion will gather the voltage from the scales as a 10 bit number. This function will convert this into a voltage and save a set of samples that can be averaged to calculate_mass, or interpreted by show_statistics.

Syntax

unsigned int * store_sample(unsigned int count, unsigned int Vss, unsigned int Vdd)

Usage

This function should be called (either directly, or by setting a flag) every time the A/D register samples the input voltage.

Input Arguments

  • unsigned int count. This is the 10b (0-1024) value from ADCON1 and ADCON2.
  • unsigned int Vss. The highest voltage that the A/D channel is recording.
  • unsigned int Vdd. The lowest voltage that the A/D channel is recording.

Output Arguments

  • None.

Return Value

  • unsigned int * . Once the voltage has been stored, the function returns a pointer to the value. (This is mainly for debugging purposes)

Settings and Registers

Read

  • None

Write

  • None

Operation

Using Vss and Vdd, the A/D sample is converted into a voltage. Then, the sample is stored in a circular buffer, where it can be read buy other parts of the program.

Code


unsigned int * store_sample(unsigned int count, unsigned int mVss, unsigned int mVdd)
{
static char vinsert = 0;
unsigned int millivolts = Vdd + (Vss*count)/1024;
unsigned int * retval = NULL;
 
ADSamples[vinsert] = millivolts;
retval = &ADSamples[vinsert];
/*advance the position in the buffer, loop around at the end*/
if(++vinsert >= VBUFSIZE)
{
  vinsert = 0;
}
return retval;
}